package HTTP;

import java.io.*;
import java.net.Socket;

/**
 * 实现HTTP服务器，观察请求效果
 **/
public class HTTPClient {
    public static void main(String[] args) throws IOException {
        //1.HTTP客户端，要发送HTTP请求，先建立TCP连接

        Socket socket=new Socket("localhost",81);
        OutputStream os=socket.getOutputStream();
        PrintWriter writer=new PrintWriter(new OutputStreamWriter(os,"UTF-8"));

        String request="GET / HTTP/1.0\r\n\r\n";//一个请求行结束
        //2.接下来写请求头部分（可以一个请求头都没有）
        //3.发送请求
        writer.print(request);
        writer.flush();//刷新

        //4.读取百度返回的HTTP响应
        InputStream is = socket.getInputStream();
        byte[] buf=new byte[10240];//我们知道响应不会超过10240字节
        int n=is.read(buf);
        String response=new String(buf,0,n,"UTF-8");
        System.out.println(response);

    }
}
